

function currentTime(){
    var $el = $(".stats").parent(),
    currentDate = new Date(),
    monthNames = [ "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio",
    "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" ],
    dayNames = ["Domingo","Lunes","Martes","Miércoles","Jueves","Viernes","Sábado"];

    $el.find(".details .big").html(monthNames[currentDate.getMonth()] + " " + currentDate.getDate() + ", " + currentDate.getFullYear());
    $el.find(".details span").last().html(dayNames[currentDate.getDay()] + ", " + currentDate.getHours()+":"+ ("0" + currentDate.getMinutes()).slice(-2));
    setTimeout(function(){
        currentTime();
    }, 10000);
}

function currentTimeNow(){
    var now = moment();

    $('.timeago').each(function(i, e) {
        var time = moment($(e).attr('datetime'));

        $(e).html('<span>' + time.from(now) + '</span>');
    });
    
    setTimeout(function(){
        currentTimeNow();
    }, 10000);
}



function UserActive(id,active){
    var url = '/admin/updateActiveUser';
    var param = 'id='+id+'&active='+active;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstUsers').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}

function UserDelete(id,deleted){
    var url = '/admin/updateDeleteUser';
    var param = 'id='+id+'&deleted='+deleted;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstUsers').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}



function ChannelActive(id,active){
    var url = '/admin/updateActiveChannel';
    var param = 'id='+id+'&active='+active;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstChannels').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}

function ChannelDelete(id,deleted){
    var url = '/admin/updateDeleteChannel';
    var param = 'id='+id+'&deleted='+deleted;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstChannels').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}


function ClusterActive(id,active){
    var url = '/admin/updateActiveCluster';
    var param = 'id='+id+'&active='+active;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstClusters').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}

function ClusterDelete(id,deleted){
    var url = '/admin/updateDeleteCluster';
    var param = 'id='+id+'&deleted='+deleted;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstClusters').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}


function CodeformatActive(id,active){
    var url = '/admin/updateActiveCodeformat';
    var param = 'id='+id+'&active='+active;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstCodeformats').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}

function CodeformatDelete(id,deleted){
    var url = '/admin/updateDeleteCodeformat';
    var param = 'id='+id+'&deleted='+deleted;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstCodeformats').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}

function ConcessionActive(id,active){
    var url = '/admin/updateActiveConcession';
    var param = 'id='+id+'&active='+active;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstConcessions').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}

function ConcessionDelete(id,deleted){
    var url = '/admin/updateDeleteConcession';
    var param = 'id='+id+'&deleted='+deleted;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstConcessions').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}






function FormatActive(id,active){
    var url = '/admin/updateActiveFormat';
    var param = 'id='+id+'&active='+active;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstFormats').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}

function FormatDelete(id,deleted){
    var url = '/admin/updateDeleteFormat';
    var param = 'id='+id+'&deleted='+deleted;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstFormats').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}



function LocationActive(id,active){
    var url = '/admin/updateActiveLocation';
    var param = 'id='+id+'&active='+active;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstLocations').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}

function LocationDelete(id,deleted){
    var url = '/admin/updateDeleteLocation';
    var param = 'id='+id+'&deleted='+deleted;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstLocations').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}

function PriceFormatActive(id,active){
    var url = '/admin/updateActivePriceFormat';
    var param = 'id='+id+'&active='+active;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstPriceFormats').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}

function PriceFormatDelete(id,deleted){
    var url = '/admin/updateDeletePriceFormat';
    var param = 'id='+id+'&deleted='+deleted;
    $.ajax({
        type: 'POST',
        url: url,
        data: param,
        dataType: 'html',
        success: function(response){
            $('#lstPriceFormats').html(response);
            getDatatables();
            resize_chosen();

        }
    });
}




function getDatatables(){
    // dataTables
    if($('.dataTable').length > 0){
        $('.dataTable').each(function(){
            var opt = {
                "sPaginationType": "full_numbers",
                "oLanguage":{
                    "sSearch": "<span>Buscar:</span> ",
                    "sInfo": "Mostrando <span>_START_</span> de <span>_END_</span> de <span>_TOTAL_</span> Registros",
                    "sLengthMenu": "_MENU_ <span>Registros por Página</span>",
                    "sFirst": "Primera",
                    "sLast": "Última",
                    "sNext": "Siguiente",
                    "sPrevious": "Anterior"
                }
            };
            if($(this).hasClass("dataTable-noheader")){
                opt.bFilter = false;
                opt.bLengthChange = false;
            }
            if($(this).hasClass("dataTable-nofooter")){
                opt.bInfo = false;
                opt.bPaginate = false;
            }
            if($(this).hasClass("dataTable-nosort")){
                var column = $(this).attr('data-nosort');
                column = column.split(',');
                for (var i = 0; i < column.length; i++) {
                    column[i] = parseInt(column[i]);
                };
                opt.aoColumnDefs =  [
                {
                    'bSortable': false, 
                    'aTargets': column
                }
                ];
            }
            if($(this).hasClass("dataTable-scroll-x")){
                opt.sScrollX = "100%";
                opt.bScrollCollapse = true;
            }
            if($(this).hasClass("dataTable-scroll-y")){
                opt.sScrollY = "300px";
                opt.bPaginate = false;
                opt.bScrollCollapse = true;
            }
            if($(this).hasClass("dataTable-reorder")){
                opt.sDom = "Rlfrtip";
            }
            if($(this).hasClass("dataTable-colvis")){
                opt.sDom = 'C<"clear">lfrtip';
                opt.oColVis = {
                    "buttonText": "Columnas <i class='icon-angle-down'></i>"
                };
            }
            if($(this).hasClass('dataTable-tools')){
                if($(this).hasClass("dataTable-colvis")){
                    opt.sDom= 'TC<"clear">lfrtip';
                } else {
                    opt.sDom= 'T<"clear">lfrtip';
                }
                opt.oTableTools = {
                    "sSwfPath": "js/plugins/datatable/swf/copy_csv_xls_pdf.swf"
                };
            }
            if($(this).hasClass("dataTable-scroller")){
                opt.sScrollY = "300px";
                opt.bDeferRender = true;
                opt.sDom = "frtiS";
                opt.sAjaxSource = "js/plugins/datatable/demo.txt";
            }
            var oTable = $(this).dataTable(opt);
            $('.dataTables_filter input').attr("placeholder", "Buscar Aquí...");
            $(".dataTables_length select").wrap("<div class='input-mini'></div>").chosen({
                disable_search_threshold: 9999999
            });
            $("#check_all").click(function(e){
                $('input', oTable.fnGetNodes()).prop('checked',this.checked);
            });
            if($(this).hasClass("dataTable-fixedcolumn")){
                new FixedColumns( oTable );
            }
            if($(this).hasClass("dataTable-columnfilter")){
                oTable.columnFilter({
                    "sPlaceHolder" : "head:after"
                });
            }
        });
    }
}

function icheck(){
    if($(".icheck-me").length > 0){
        $(".icheck-me").each(function(){
            var $el = $(this);
            var skin = ($el.attr('data-skin') !== undefined) ? "_"+$el.attr('data-skin') : "",
            color = ($el.attr('data-color') !== undefined) ? "-"+$el.attr('data-color') : "";

            var opt = {
                checkboxClass: 'icheckbox' + skin + color,
                radioClass: 'iradio' + skin + color,
                increaseArea: "10%"
            }

            $el.iCheck(opt);
        });
    }
}
    
$(document).ready(function() {
    currentTime();
    currentTimeNow();
    
    
    // Validation
    if($('.form-validate').length > 0)
    {
        $('.form-validate').each(function(){
            var id = $(this).attr('id');
            $("#"+id).validate({
                errorElement:'span',
                errorClass: 'help-inline error',
                errorPlacement:function(error, element){
                    element.parents('.controls').append(error);
                },
                highlight: function(label) {
                    $(label).closest('.control-group').removeClass('error success').addClass('error');
                },
                success: function(label) {
                    label.addClass('valid').closest('.control-group').removeClass('error success').addClass('success');
                }
            });
        });
    }
    
    
    getDatatables();
    // force correct width for chosen
    resize_chosen();



});

$(window).resize(function(){
    // console.log($(window).width());
    });